How to Fix Image Colors for Reading

This article was translated from English: Does it need improvement?
Translated
View the article in English

修正圖像顏色涉及多種技術來提高圖像的可讀性和質量。 IronOcr 提供二值化、灰度、反相和顏色替換等方法以使圖像中的文字和內容更易讀且更美觀,這在使用 OCR(光學字符識別)從圖像中提取文字時尤為重要。 也可以僅讀取所選的文字顏色。

快速開始:一次隔離特定的文字顏色

使用 IronOCR 的 SelectTextColor 方法將 OCR 重點放在您關心的文字顏色上,無需複雜的圖像處理。 在一行代碼中,您可以加載圖像,選擇文字顏色和容差,並僅提取該文字以獲得準確的 OCR 結果。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    new IronTesseract().Read(new IronOcr.OcrImageInput("sample.jpg").SelectTextColor(new IronSoftware.Drawing.Color("#DB645C"), 60));
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5步驟)

  1. 下載 C# 庫來修正圖像顏色
  2. 匯入 PDF 文件和圖像以供閱讀
  3. 應用所需的顏色效果,例如二值化、灰度、反相和顏色替換
  4. 匯出經修正的圖像以供查看
  5. 使用 SelectTextColor 方法讀取特定的文字顏色


二值化圖像範例

此過程將圖像轉換成兩色格式,通常是黑白的。這對於從背景中分離文字和減少噪音很有用,使文字更加明確且易於閱讀。

要對圖像應用二值化效果,請使用 Binarize 方法。 由於 OCR 過程在對比度最高的圖像上表現最佳,其中黑色文字在白色背景上,因此此方法在使背景與字符非常區分開方面顯得尤為重要。

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-binarize-image.cs
using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Apply binarize affect
imageInput.Binarize();

// Export the modified image
imageInput.SaveAsImages("binarize.jpg");
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Apply binarize affect
imageInput.Binarize()

' Export the modified image
imageInput.SaveAsImages("binarize.jpg")
$vbLabelText   $csharpLabel

為了方便起見,可以使用 SaveAsImages 方法匯出修改後的圖像。 下面是圖像在二值化之前和之後的對比。

class="competitors-section__wrapper-even-1">
Sample image
Binarized image

灰度化圖像範例

將圖像轉換成不同階段的灰色可以使其不那麼分散注意力,且更易於閱讀。 這在原圖中的顏色造成視覺混亂時特別有幫助。

要對圖像應用灰度效果,請使用 ToGrayScale 方法。 灰度過程涉及取 R、G 和 B 值的平均值。

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-grayscale-image.cs
// Apply grayscale affect
imageInput.ToGrayScale();
' Apply grayscale affect
imageInput.ToGrayScale()
$vbLabelText   $csharpLabel
class="competitors-section__wrapper-even-1">
Sample image
Grayscaled image

反轉圖像範例

反轉顏色可以增強對比度。例如,將黑色背景上的白色文字變成白色背景上的黑色文字可以改善可讀性。

使用 Invert 方法來反轉圖像顏色。 該方法可選擇接受布爾值,用於刪除所有色彩通道並返回灰度圖像。

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-invert-image.cs
// Apply invert affect
imageInput.Invert();
' Apply invert affect
imageInput.Invert()
$vbLabelText   $csharpLabel

下面的圖像顯示了帶有和不帶有灰度選項的反轉方法。

class="competitors-section__wrapper-even-1">
Inverted image
Inverted and grayscaled image

替換顏色範例

此技術允許您使用其他顏色替換圖像中特定的顏色,這可以幫助突出或減少某些元素的強調。 它通常用於使文字更突出或修正有問題的顏色對比。

要使用 ReplaceColor 方法,請指定要替換的顏色以及新顏色。 此方法的第三個參數,即對應的容差值,也很重要。 在模糊圖像中需要較高的容差以達到預期的結果。

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-replace-color.cs
using IronOcr;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
IronSoftware.Drawing.Color currentColor = new IronSoftware.Drawing.Color("#DB645C");
IronSoftware.Drawing.Color newColor = IronSoftware.Drawing.Color.DarkCyan;

// Replace color
imageInput.ReplaceColor(currentColor, newColor, 80);

// Export the modified image
imageInput.SaveAsImages("replaceColor");
Imports IronOcr

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
Private currentColor As New IronSoftware.Drawing.Color("#DB645C")
Private newColor As IronSoftware.Drawing.Color = IronSoftware.Drawing.Color.DarkCyan

' Replace color
imageInput.ReplaceColor(currentColor, newColor, 80)

' Export the modified image
imageInput.SaveAsImages("replaceColor")
$vbLabelText   $csharpLabel
class="competitors-section__wrapper-even-1">
Sample image
Replaced color image

讀取特定文字顏色範例

此功能旨在僅讀取指定的文字顏色。 使用 SelectTextColor 方法指定 IronOcr 將專注於的顏色以及容差值。 容差值接受 0-255 的範圍,這表示顏色空間中像素顏色與所選顏色在每個 R、G 和 B 值之間的允許差異。

:path=/static-assets/ocr/content-code-examples/how-to/image-color-correction-select-text-color.cs
using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Add image
using var imageInput = new OcrImageInput("sample.jpg");
// Text color to focus on
IronSoftware.Drawing.Color focusColor = new IronSoftware.Drawing.Color("#DB645C");

// Specify which text color to read
imageInput.SelectTextColor(focusColor, 60);

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output result to console
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports System

' Instantiate IronTesseract
Private ocrTesseract As New IronTesseract()

' Add image
Private imageInput = New OcrImageInput("sample.jpg")
' Text color to focus on
Private focusColor As New IronSoftware.Drawing.Color("#DB645C")

' Specify which text color to read
imageInput.SelectTextColor(focusColor, 60)

' Perform OCR
Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput)

' Output result to console
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

下面,您將找到 OCR 結果,旨在僅讀取橙色系顏色的文字。

class="content-img-align-center">
class="center-image-wrapper"> OCR result

可搜索的 PDF

除了提供圖像過濾選項外,IronOcr 還使開發人員能夠儲存已修改的 PDF,無論是否應用這些過濾器。 SaveAsSearchablePdf 方法的第二個參數是一個布爾標誌,它允許用戶指定是否在激活或停用過濾器的情況下儲存 PDF。

:path=/static-assets/ocr/content-code-examples/how-to/image-quality-correction-searchable-pdf.cs
using IronOcr;

var ocr = new IronTesseract();
var ocrInput = new OcrInput();

// Load a PDF file
ocrInput.LoadPdf("invoice.pdf");

// Apply gray scale filter
ocrInput.ToGrayScale();
OcrResult result = ocr.Read(ocrInput);

// Save the result as a searchable PDF with filters applied
result.SaveAsSearchablePdf("outputGrayscale.pdf", true);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

常見問題解答

如何在.NET C#中增強影像顏色以進行OCR辨識?

您可以使用 IronOCR 在 .NET C# 中增強影像顏色以進行 OCR 識別。首先從 NuGet 下載 IronOCR 庫,導入圖像,然後應用二值化、灰度化和反轉等顏色校正技術來提高文字可讀性。

二值化在影像顏色校正中扮演什麼角色?

影像色彩校正中的二值化將影像轉換為黑白格式,這有助於將文字與背景分離,減少噪聲,使文字更加清晰,從而在使用 IronOCR 時獲得更好的 OCR 效果。

應用灰階效果如何使OCR辨識過程受益?

應用灰階效果可以將影像轉換為灰度,從而減少色彩幹擾。這種簡化處理使影像更簡潔,增強了文字的可讀性,有利於使用 IronOCR 進行 OCR 辨識。

反轉影像顏色進行文字擷取有哪些優點?

反轉影像顏色可以透過改變配色方案來增加對比度,例如將黑底白字改為白底黑字。這種對比的提升可以提高使用 IronOCR 進行 OCR 時的文字擷取準確率。

如何更改影像中的特定顏色以獲得更好的 OCR 識別結果?

若要變更影像中的特定顏色以獲得更好的 OCR 效果,請使用 IronOCR 中的ReplaceColor方法。指定原始顏色、新顏色,並調整容差值以微調顏色替換。

在OCR辨識過程中,是否可以聚焦於特定顏色的文字?

是的,您可以使用 IronOCR 中的SelectTextColor方法在 OCR 流程中識別特定顏色的文字。這樣,您可以指定目標文字顏色和容差值,從而提高彩色文字的 OCR 準確率。

色彩校正後,如何匯出修改後的影像?

對影像進行色彩校正後,您可以使用 IronOCR 中的SaveAsImages功能匯出影像。這樣,您可以將修改後的影像保存下來,以便進行後續處理或檢視。

為什麼調整影像對比對OCR辨識準確率很重要?

調整影像對比度對於 OCR 的準確性至關重要,因為它有助於將文字與背景區分開來,使字元更加突出,更容易被 OCR 演算法提取,尤其是在使用 IronOCR 時。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

審核人

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 70

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 70
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

">

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 84

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 84
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once